<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{ titleBar }}记录</block>
		</cu-custom>
		<view>
			<tab :tabs="tabs" @change="changeTab" v-model="tabCur"></tab>
		</view>
		<!-- <view class="search bg-white m-all20 border-radius" v-if="tabCur == '1'">
			<view class="icon cuIcon-search mt10 mb20 ml20"></view>
			<view class="input mt10 mb20">
				<input v-model="phone" clear :maxlength="11" type="text" placeholder="请输入手机号" />
			</view>
			<view class="btn" @tap.stop="search">搜索</view>
		</view> -->
		<view>
			<view v-if="infoType == 'pointsCurrent'">
				<view v-for="(item, index) in tabData" :key="index" class="bg-white p-all20 m-all20 border-radius">
					<view class="f36">
						<text>直播名称:</text>
						<text>{{ item.liveName || '暂无' }}</text>
					</view>
					<view class="f30 mt10">
						<text>直播间编号:</text>
						<text>{{ item.liveNo }}</text>
					</view>
					<view class="f30 mt10">
						<text>团长名称:</text>
						<text>{{ item.tzNickName || '暂无' }}</text>
					</view>
					<view class="f30 mt10">
						<text>团长手机号:</text>
						<text>{{ item.tzPhone || '暂无' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>用户名:</text>
						<text>{{ item.nickName || '暂无' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>手机号:</text>
						<text>{{ item.phone || '暂无' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>时间:</text>
						<text>{{ item.createTime || '暂无' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>获取类型:</text>
						<text>{{ item.description || '暂无' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>积分数:</text>
						<text>{{ item.rewardNum || '0' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>发放数量:</text>
						<text>{{ item.getUserNo || '0' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>发放总积分:</text>
						<text>{{ item.rewardSum || '0' }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>用户当前积分:</text>
						<text>{{ item.pointsCurrent || 0 }}</text>
					</view>
					<view class="f30 mt10 flex-s-b" v-if="tabCur == '1'">
						<text>操作</text>
						<text
							class="cu-btn border-radius"
							@tap.stop="$goUrl('/pages/user/edit-points/index?id=' + item.userId + '&sum=' + item.pointsCurrent)"
							:class="'bg-' + theme.backgroundColor"
						>
							编辑
						</text>
					</view>
				</view>
			</view>
			<view v-if="infoType == 'couponNum'">
				<view v-for="(item, index) in tabData" :key="index" class="bg-white p-all20 m-all20 border-radius">
					<view class="f36">
						<text>直播名称:</text>
						<text>{{ item.liveName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>直播间编号:</text>
						<text>{{ item.liveNo ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>用户名:</text>
						<text>{{ item.nickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>手机号:</text>
						<text>{{ item.phone ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长名称:</text>
						<text>{{ item.tzNickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长手机号:</text>
						<text>{{ item.tzPhone ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>时间:</text>
						<text>{{ item.createTime ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>优惠券:</text>
						<text>{{ item.couponName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>库存:</text>
						<text>{{ item.stock ||'0'}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>核销状态:</text>
						<text>{{ item.checkFlag == 1 ? '已核销' : '未核销' }}</text>
					</view>
					<view class="f30 mt10 flex-s-b" v-if="tabCur == '1' && item.checkFlag == 0">
						<text>操作</text>
						<text class="cu-btn border-radius" @tap.stop="handCouponCheckPut(item)" :class="'bg-' + theme.backgroundColor">核销</text>
					</view>
				</view>
			</view>
			<view v-if="infoType == 'redPrice'">
				<view v-for="(item, index) in tabData" :key="index" class="bg-white p-all20 m-all20 border-radius">
					<view class="f36">
						<text>直播名称:</text>
						<text>{{ item.liveName||"暂无" }}</text>
					</view>
					<view class="f30 mt10">
						<text>直播间编号:</text>
						<text>{{ item.liveNo ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长名称:</text>
						<text>{{ item.tzNickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长手机号:</text>
						<text>{{ item.tzPhone ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>用户名:</text>
						<text>{{ item.nickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>手机号:</text>
						<text>{{ item.phone||"暂无" }}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>时间:</text>
						<text>{{ item.createTime ||"暂无"}}</text>
					</view>

					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>金额:</text>
						<text>{{ item.totalAmount ||''}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>金额:</text>
						<text>{{ item.rewardNum ||0}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>发放数量:</text>
						<text>{{ item.getUserNo ||'0'}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>总金额:</text>
						<text>{{ item.rewardSum ||'0'}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>核销状态:</text>
						<text>{{ item.checkFlag == 1 ? '已核销' : '未核销' }}</text>
					</view>
					<view class="f30 mt10 flex-s-b" v-if="tabCur == '1' && item.checkFlag == 0">
						<text>操作</text>
						<text class="cu-btn border-radius" :class="'bg-' + theme.backgroundColor">核销</text>
					</view>
				</view>
			</view>
			<view v-if="infoType == 'prizeNum'">
				<view v-for="(item, index) in tabData" :key="index" class="bg-white p-all20 m-all20 border-radius">
					<view class="f36">
						<text>直播名称:</text>
						<text>{{ item.liveName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>直播间编号:</text>
						<text>{{ item.liveNo ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长名称:</text>
						<text>{{ item.tzNickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10">
						<text>团长手机号:</text>
						<text>{{ item.tzPhone ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>用户名:</text>
						<text>{{ item.nickName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>手机号:</text>
						<text>{{ item.phone ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>时间:</text>
						<text>{{ item.createTime }}</text>
					</view>

					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>奖品:</text>
						<text>{{ item.lotteryName ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>奖品:</text>
						<text>{{ item.name ||"暂无"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '2'">
						<text>数量:</text>
						<text>{{ item.qty ||"0"}}</text>
					</view>
					<view class="f30 mt10" v-if="tabCur == '1'">
						<text>核销状态:</text>
						<text>{{ checkFlagDic[item.checkFlag] }}</text>
					</view>
					<view class="f30 mt10 flex-s-b" v-if="tabCur == '1' && item.checkFlag == 0">
						<text>操作</text>
						<text class="cu-btn border-radius" @tap.stop="handIvelotteryPut(item)" :class="'bg-' + theme.backgroundColor">核销</text>
					</view>
				</view>
			</view>
			<view :class="'cu-load bg-gray ' + (loadmore ? 'loading' : 'over')"></view>
		</view>
		<userDataSearch :options="formOptions" @searchChange="searchChange"></userDataSearch>
	</view>
</template>

<script>
const app = getApp();
import api from 'utils/api';
import tab from '@/components/tab/index.vue';
const util = require('utils/util.js');
import userDataSearch from '../user-data-search/index.vue';
export default {
	components: {
		tab,
		userDataSearch
	},
	data() {
		return {
			checkFlagDic: {
				0: '未核销',
				1: '已核销',
				2: '未中奖'
			},
			theme: app.globalData.theme,

			tabCur: '1',
			tabs: [
				{
					label: '用户领取',
					value: '1'
				},
				{
					label: '我发放的',
					value: '2'
				}
			],
			titleBar: '',
			tabData: [],
			page: {
				current: 1,
				size: 15
				//升序字段
			},
			infoType: '',
			loadmore: false,
			phone: '',
			searchForm: {},
			formOptions: [
				// {
				// 	label: '用户或团长手机号',
				// 	name: 'phone'
				// },
				{
					label: '用户名称',
					name: 'nickName'
				},
				{
					label: '团长名称',
					name: 'tzNickName'
				},
				{
					label: '团长手机号',
					name: 'tzPhone'
				},
				{
					label: '直播间名称',
					name: 'liveName'
				},
				{
					label: '直播间编号',
					name: 'liveNo'
				},
				{
					label: '开始时间',
					name: 'startTime',
					type: 'date'
				},
				{
					label: '结束时间',
					name: 'endTime',
					type: 'date'
				}
			]
		};
	},

	onReachBottom() {
		if (this.loadmore) {
			this.page.current = this.page.current + 1;
			this.init();
		}
	},
	onLoad(options) {
		this.titleBar = this.typeDictionary(options.type);
		this.infoType = options.type;
		this.userInfo = uni.getStorageSync('user_info');
		if (this.infoType == 'couponNum') {
			this.formOptions.unshift({
				label: '优惠券名称',
				name: 'couponName'
			});
		}
		if (this.infoType == 'prizeNum') {
			this.formOptions.unshift({
				label: '奖品名称',
				name: 'lotteryName'
			});
		}
	},
	onShow(options) {
		this.refresh();
	},
	methods: {
		searchChange(e) {
			this.searchForm = e;
			this.searchForm.startTime = e.startTime ? e.startTime : '';
			this.searchForm.endTime = e.endTime ? e.endTime : '';
			this.tabData = [];
			this.init();
		},
		handIvelotteryPut(item) {
			uni.showModal({
				title: '提示',
				content: '是否核销奖品?',
				showCancel: true,
				success: ({ confirm, cancel }) => {
					if (confirm) {
						api.ivelotteryPut({
							id: item.id
						}).then((res) => {
							uni.showToast({
								title: '操作成功',
								icon: 'success',
								mask: true
							});
							this.refresh();
						});
					}
				}
			});
		},
		handCouponCheckPut(item) {
			uni.showModal({
				title: '提示',
				content: '是否核销优惠券?',
				showCancel: true,
				success: ({ confirm, cancel }) => {
					if (confirm) {
						api.couponCheckPut({
							id: item.id
						}).then((res) => {
							uni.showToast({
								title: '操作成功',
								icon: 'success',
								mask: true
							});
							this.refresh();
						});
					}
				}
			});
		},
		changeTab() {
			this.refresh();
		},
		search() {
			this.refresh();
		},
		typeDictionary(type) {
			let obj = {
				pointsCurrent: '积分',
				redPrice: '红包',
				couponNum: '优惠券',
				prizeNum: '奖品'
			};
			return obj[type];
		},
		//刷新
		refresh() {
			this.page.current = 1;
			this.tabData = [];
			this.init();
		},
		// 初始化
		init() {
			this.loadmore = true;
			if (this.infoType == 'pointsCurrent') {
				if (this.tabCur == 1) {
					this.pointsRecordPage();
				} else {
					this.tzLiverewardrecord();
				}
			}
			if (this.infoType == 'redPrice') {
				if (this.tabCur == 1) {
					this.liverewardrecord();
				} else {
					this.tzLiveRedrecord();
				}
			}
			if (this.infoType == 'couponNum') {
				if (this.tabCur == 1) {
					this.livecouponrecord();
				} else {
					this.tzLvecouponrecord();
				}
			}
			if (this.infoType == 'prizeNum') {
				if (this.tabCur == 1) {
					this.livelotteryjoinrecord();
				} else {
					this.tzLivelotteryjoinrecord();
				}
			}
		},
		tzLiverewardrecord() {
			let para = Object.assign(
				{
					rewardType: '1',
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.tzLiverewardrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		tzLiveRedrecord() {
			let para = Object.assign(
				{
					rewardType: '2',
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.tzLiverewardrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		tzLvecouponrecord() {
			let para = Object.assign(
				{
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.tzLvecouponrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		tzLivelotteryjoinrecord() {
			let para = Object.assign(
				{
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.tzLivelotteryjoinrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		pointsRecordPage() {
			let para = Object.assign(
				{
					rewardType: '1',
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.pointsRecordPage(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		liverewardrecord() {
			let para = Object.assign(
				{
					rewardType: '2',
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.liverewardrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		livecouponrecord() {
			let para = Object.assign(
				{
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.livecouponrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		},
		livelotteryjoinrecord() {
			let para = Object.assign(
				{
					phone: this.phone
				},
				this.searchForm,
				this.page
			);
			api.livelotteryjoinrecord(util.filterForm(para)).then((res) => {
				let pointsRecord = res.data.records;
				this.tabData = [...this.tabData, ...pointsRecord];
				if (pointsRecord.length < this.page.size) {
					this.loadmore = false;
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.search {
	display: flex;
	align-items: center;
	border-radius: 50rpx;

	.input {
		flex: 1;
		margin: 0 20rpx;

		input {
			min-height: 70rpx;
		}
	}

	.btn {
		padding: 0 40rpx;
		margin: 10rpx 0;
		margin-right: 10rpx;
		color: #fff;
		height: 70rpx;
		background-color: #007aff;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
}
</style>
